package com.nepu.gmall.realtime.util;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 这里是线程池，使用单例的方式创建
 * @author chenshuaijun
 * @create 2023-03-06 10:05
 */
public class ThreadPoolUtil {

    private static ThreadPoolExecutor threadPoolExecutor;

    public static ThreadPoolExecutor getThreadPoolExecutor(){
        if (threadPoolExecutor == null){
            synchronized (ThreadPoolUtil.class){
                if (threadPoolExecutor == null){
                    // 线程池的参数：
                    //  int corePoolSize 常驻线程
                    //  int maximumPoolSize 最大线程数
                    //  long keepAliveTime 线程存活时间。如果线程空闲时间超出存活时间,线程就会被回收
                    //  TimeUnit unit 存活时间单位
                    //  BlockingQueue<Runnable> workQueue 工作队列，所有的请求都会先被放入到队列中
                    threadPoolExecutor = new ThreadPoolExecutor(4, 20, 60 * 5, TimeUnit.SECONDS,new LinkedBlockingDeque<Runnable>(Integer.MAX_VALUE));
                }
            }
        }
        return threadPoolExecutor;
    }
}
